{% extends "correspondence/base.html" %}

{% block page_link %}
<!--    <script src="../../static/correspondence/js/write.js"></script>-->
    <link rel="stylesheet" href="../../static/announcements/css/style.css">
    <link rel="stylesheet" href="../../static/system_setup/css/style.css">
    <link rel="stylesheet" href="../../static/public_file/css/PersonnelChooser.css">
    <script src="../../static/public_file/js/wangEditor.min.js" type="text/javascript" charset="UTF-8"></script>
{% endblock %}


{% block style %}
    <style>
        .add_class{
            background: #1E90FF;
        }
        .file {
            position: relative;
            display: inline-block;
            background: #D0EEFF;
            border: 1px solid #99D3F5;
            border-radius: 4px;
            padding: 4px 12px;
            overflow: hidden;
            color: #1E88C7;
            text-decoration: none;
            text-indent: 0;
            line-height: 20px;
        }
        .file input {
            position: absolute;
            font-size: 100px;
            right: 0;
            top: 0;
            opacity: 0;
        }
        .file:hover {
            background: #AADFFD;
            border-color: #78C3F3;
            color: #004974;
            text-decoration: none;
        }
        tr{
            height: 35px;
        }
        th{
            text-align: center;
        }
        table {
            table-layout: fixed;
        }

        input{
            padding: 8px 10px 8px 40px;
        }

        #ThreeNavigation div {
            padding-left: 2%;
        }
    </style>
{% endblock %}


{% block right_content %}
    <div class="headline">
        <div style="width: 15%;height: 100%;line-height: 40px;float: left;padding-left: 1%;">
            <a href="home.html" style="color: #0b0b0b;">
                <i class="iconfont icon-tongzhigonggao"></i>
                <span> 通信</span> <span> > </span> <a href="send-message"> 写信</a>
            </a>
        </div>
        <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button style="height: 30px;width: 90%;background: #0070c1;color: #ffffff;border-radius: 3px" id="add_station" type="button" class="btn btn-default add-station" onclick="send_message('False')">发送</button>
        </div>
          <div style="width: 7%;text-align: center;height: 100%;line-height: 40px;float: right;">
            <button style="height: 30px;width: 90%;background: #0070c1;color: #ffffff;border-radius: 3px" id="save_station" type="button" class="btn btn-default add-station" onclick="send_message('True')">存草稿</button>
        </div>
    </div>

    <div class="right-box" style="width: 80%;height: 93%;padding: 0 30px 0 15px;overflow: auto;margin-left: 10%;padding-left: 5%;">
        <div style="margin-top: 3%;width: 100%;">
            <span style="color: red;">*</span>
            <label> 收信人账号：</label>
            <input id="recipient"  {% if reply_flag != 'true' %}onclick="model_line()"{% endif %} type="text" placeholder=" 收信人账号" style="width: 80%;padding-left: 1%;outline: none;border: none;border-bottom: #727E8C 1px solid;height: 35px;" value="{{ recipient }}" {% if reply_flag == 'true' %}readonly{% endif %} autocomplete="off">
        </div>

        <div style="margin-top: 2%;width: 100%;">
            <span style="color: red;">*</span>
            <label> 信 件 主 题：</label>
            <input id="title" type="text" placeholder=" 信 件 主 题" style="width: 80%;padding-left: 1%;outline: none;border: none;border-bottom: #727E8C 1px solid;height: 35px;" value="{{ title }}" {% if reply_flag == 'true' %} readonly {% endif %}>
        </div>


        <div id="receive" style="width: 100%;margin-top: 2%;">
            <span style="color: red;">*</span>
            <label style="vertical-align: top;"> 信 件 内 容：</label>
            <div id="summernote" style="width: 80%;display: inline-block;vertical-align: top;">
                {% autoescape off %}
                    {{ message_content }}
                {% endautoescape %}
            </div>
        </div>

        <div style="margin-top: 2%;width: 100%;">
            <span style="color: red;visibility: hidden;">*</span>
            <label style="vertical-align: top;"> 附&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;件：</label>
            <a href="javascript:;" class="file" style="display: inline-block;vertical-align: top;">
                上传附件
                <input type="file" name="party_main_body" id="party_main_body" onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
            </a>
        </div>

        <div style="margin-top: 2%;width: 100%;">
            <span style="color: red;visibility: hidden;">*</span>
            <div style="border: #00d0ff solid 1px;display: inline-block;vertical-align: top;width: 86%;">
                <table class="table table-hover table-bordered" style="border-collapse: collapse;width: 100%;">
                    <thead>
                        <tr>
                            <th scope="col" style="width: 8%;">序号</th>
                            <th scope="col" style="width: 30%;">文件名称</th>
                            <th scope="col" style="width: 8%;">文件大小</th>
                            <th scope="col" style="width: 15%;">上传时间</th>
                            <th scope="col" style="width: 10%;">上传人员</th>
                            <th scope="col" style="width: 5%;">操作</th>
                        </tr>
                    </thead>
                    <tbody style="overflow: auto;" id="party_main">
                        {% for file in file_list %}
                            <tr>
                                <td style="text-align: center;">{{ file.serial }}</td>
                                <td style="padding-left: 5px;">{{ file.file_name }}</td>
                                <td style="text-align: center;">{{ file.file_size }}</td>
                                <td style="text-align: center;">{{ file.create_time }}</td>
                                <td style="text-align: center;">{{ file.uploader_user }}</td>
                                <td style="text-align: center;"><a href="{{ file.pdf_path }}">查看</a> | <a href="#" onclick="RemoveTr(this, 'party_main', '{{ file.id }}')">删除</a></td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>
            </div>
        </div>


        <div style="height: 50px;">

        </div>
    </div>

    <div id="staff-select" class="shade">
        <div id="staff-model" style="width: 100%;height: 100%;position: absolute;">
            <div style="width: 900px;height: 500px;background: #F5F5F5;z-index: 10000;position: absolute;top: 50%;left: 50%;transform:translate(-50%,-50%);border-radius: 3px;border: #dfdfdf solid 1px;">
                <div id="staff-box" style="width: 100%;height: 40px;background: #f4f7fa;cursor: move;border-top-left-radius: 5px;border-top-right-radius: 5px;border-bottom: #dfdfdf solid 1px;">
                    <div style="width: 20%;margin-left: 1%;display: inline-block;line-height: 40px;font-size: 14px;">
                        选择人员
                    </div>
                    <span onclick="close_model()" style="float: right;padding-right: 2%;cursor: pointer;line-height: 37px;">
                        <i class="iconfont icon-quchu"></i>
                    </span>
                </div>
                <div style="height: 460px;width: 100%;border-bottom: #dfdfdf solid 1px;">
                    <div style="width: 70px;height: 100%;border-right: #cfcfcf solid 1px;display: inline-block;vertical-align: top;">
                        <div id="dep" class="navigation navigation_color" onclick="change_navigation(this)">部门</div>
                        <div id="limits" class="navigation" onclick="change_navigation(this)">权限</div>
                        <div id="fun" class="navigation" onclick="change_navigation(this)">职能</div>
                    </div>
                    <div style="width: 200px;height: 100%;border-right: #cfcfcf solid 1px;display: inline-block;vertical-align: top;">
                        <div style="width: 100%;height: 50px;line-height: 50px;">
                            <input id="SearchDep" onkeydown="SearchDepartment()" class="search_input" type="text" placeholder="搜索" autocomplete="off" style="width: 95%;">
                        </div>
                        <div id="SecondaryNavigation" style="width: 100%;height: 403px;line-height: 50px;overflow: auto;"></div>
                    </div>
                    <div style="width: 200px;height: 100%;border-right: #cfcfcf solid 1px;display: inline-block;vertical-align: top;">
                        <div style="width: 100%;height: 50px;line-height: 50px;padding-left: 2%;">
                            <input type="checkbox" id="select-all" onclick="SelectAll(this)">
                            <input id="SearchPeo" onkeydown="SearchProple()" class="search_input" type="text" placeholder="搜索" autocomplete="off" style="width: 87%;">
                        </div>
                        <div id="ThreeNavigation" style="width: 100%;height: 403px;line-height: 50px;overflow: auto;"></div>
                    </div>
                    <div style="width: 410px;height: 100%;display: inline-block;vertical-align: top;overflow: auto">
                        <div style="width: 100%;height: 40px;text-align: left;font-size: 14px;line-height: 40px;">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;已选 <span id="selected-number">0</span> 人
                        </div>
                        <div id="selected-div" style="width: 100%;font-size: 14px;height: 370px;overflow: auto;line-height: 20px;">

                        </div>
                        <div style="width: 100%;font-size: 14px;height: 40px;text-align: center;">
                            <button onclick="IdentifyPersonnel()" style="width: 100px;height: 30px;line-height: 25px;text-align: center;letter-spacing: 5px;border-radius: 3px;border: #cfcfcf solid 1px;font-size: 14px;cursor: pointer;margin-top: 10px;">完成</button>
                            <button onclick="close_model()" style="margin-left: 15px;width: 100px;height: 30px;line-height: 25px;text-align: center;color: #0abb78;letter-spacing: 5px;border-radius: 3px;font-size: 14px;border: #cfcfcf solid 1px;cursor: pointer;margin-top: 10px;">取消</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="del_adjunct" style="z-index: 1000;position: fixed;background-color: rgba(36, 36, 36, 0.5);width: 100%;height: 100%;top: 0;left: 0;right: 0;bottom: 0;display: none;">
        <div style="top: 50%;left: 50%;width: 400px;height: 150px;background: white;transform:translate(-50%,-50%);border: #dfdfdf solid 1px;position: absolute;border-radius: 4px;">
            <div style="width: 100%;height: 100px;text-align: center;line-height: 100px;">请查看附件，删除无用的附件！</div>
            <hr>
            <div style="width: 100%;height: 50px;text-align: right;padding-right: 5%;">
                <button onclick="del_adjunct_button()" style="width: 50px;height: 30px;line-height: 30px;margin-top: 10px;background: #00a0e9;color: white;border-radius: 4px;border: #00a0e9 solid 1px;">确认</button>
            </div>
        </div>
    </div>
{% endblock %}

{% block script %}
    <script type="text/javascript" src="../../static/public_file/js/PersonnelChoosersNew.js" charset="gb2312"></script>
    <script>
        window.onload = function () {
            document.getElementById("correspondence").className = "add-nav-active";
        };

        var unique = '{{ unique | safe }}';

        const E = window.wangEditor;
        const editor = new E("#summernote");
        // 或者 const editor = new E(document.getElementById('div1'))
        editor.config.uploadImgShowBase64 = 'true';
        editor.placeholder = "qin";
        editor.config.zIndex = 1;
        editor.create();

        // 上传正文
        function upload_accessory(file_type, tbody_id, accessory_type) {
            document.getElementById("loading").style.display = "inline-block";
            var form = new FormData();
            var files = document.getElementById(accessory_type).files[0];
            form.append(file_type, files);
            form.append('number', unique)
            form.append("source", "yx")
            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/topics_attachments_upload", true);
            xhr.send(form);
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        if (JsonDate["code"] === 400){
                            alert("请上传xlsx格式文件！")
                        }
                        creat_table(JsonDate, tbody_id);
                        document.getElementById("party_main_body").value="";
                    }
                }
            };
        }

        // 创建附件列表
        function creat_table(JsonData, tbody_id) {
            var t_table = document.getElementById(tbody_id);
            var local_host = window.location.host;
            if (Array.isArray(JsonData["pdf_path"])){
                for (var i=0; i<JsonData["pdf_path"].length; i++){
                    var tr = document.createElement("tr");
                    var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
                    td1.innerHTML = t_table.children.length+1;td1.style.textAlign = "center";td1.style.border="#cccccc solid 1px";td1.style.borderTop="none";
                    td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border="#cccccc solid 1px";td2.style.borderLeft="none";td2.style.borderTop="none";
                    td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border="#cccccc solid 1px";td3.style.borderLeft="none";td3.style.borderTop="none";
                    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border="#cccccc solid 1px";td4.style.borderLeft="none";td4.style.borderTop="none";
                    td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border="#cccccc solid 1px";td5.style.borderLeft="none";td5.style.borderTop="none";
                    td6.innerHTML = "<a target='_blank' href='"+JsonData["pdf_path"][i]["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \""+tbody_id+"\", \""+JsonData["pdf_path"][i]["id"]+"\")'>删除</a>";td6.style.textAlign = "center";td6.style.border="#cccccc solid 1px";td6.style.borderLeft="none";td6.style.borderTop="none";
                    tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                    t_table.appendChild(tr);
                }
                if (JsonData["pdf_path"].length > 1){
                    document.getElementById("del_adjunct").style.display='block';
                }
            }else {
                tr = document.createElement("tr");
                td1 = document.createElement("td");td2 = document.createElement("td");td3 = document.createElement("td");td4 = document.createElement("td");td5 = document.createElement("td");td6 = document.createElement("td");
                td1.innerHTML = t_table.children.length+1;td1.style.textAlign = "center";td1.style.border="#cccccc solid 1px";td1.style.borderTop="none";
                td2.innerHTML = JsonData["file_name"];td2.style.paddingLeft = "1%";td2.style.border="#cccccc solid 1px";td2.style.borderLeft="none";td2.style.borderTop="none";
                td3.innerHTML = JsonData["file_size"];td3.style.textAlign = "center";td3.style.border="#cccccc solid 1px";td3.style.borderLeft="none";td3.style.borderTop="none";
                td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";td4.style.border="#cccccc solid 1px";td4.style.borderLeft="none";td4.style.borderTop="none";
                td5.innerHTML = JsonData["uploader"];td5.style.textAlign = "center";td5.style.border="#cccccc solid 1px";td5.style.borderLeft="none";td5.style.borderTop="none";
                td6.innerHTML = "<a target='_blank' href='"+JsonData["pdf_path"] + "'>查看</a> | <a style='color: red;' href='#' onclick='RemoveTr(this, \""+tbody_id+"\", \""+JsonData["id"]+"\")'>删除</a>";td6.style.textAlign = "center";td6.style.border="#cccccc solid 1px";td6.style.borderLeft="none";td6.style.borderTop="none";
                tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
                t_table.appendChild(tr);
            }
        }

        function del_adjunct_button() {
            document.getElementById("del_adjunct").style.display='none';
        }


        function RemoveTr(_this, tbody_id, wj_id) {
            var table = document.getElementById(tbody_id);
            var trNode = $(_this).parent().parent();
            trNode.remove();

            for (var i=0; i<table.children.length; i++){
                table.children[i].children[0].innerHTML=i+1;
            }

            var xhr = new XMLHttpRequest();
            var local_host = window.location.host;          // 获取当前IP
            xhr.open('post', "/administrative/delete_attachments_view", true);
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            xhr.send("id="+String(wj_id));
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display = "none";
                        var JsonDate = JSON.parse(xhr.responseText);
                        console.log(JsonDate);
                        alert("删除成功！")
                    }
                }
            };
        }

        if (window.location.href.split("recipient=").length>1){
            document.getElementById('recipient').value = window.location.href.split("recipient=")[1];
            document.getElementById('recipient').setAttribute("disabled", "ture");
            document.getElementById('choose_recipient').style.display='none';
        }

        function send_message(types) {
            var form = new FormData();
            var recipient = document.getElementById("recipient").value;
            var title = document.getElementById("title").value;
            var message_content = editor.txt.html().replace("&", "%26");
            var Plain_Text = editor.txt.text().replace(/&nbsp;/gi, "");


            if (!recipient){
                alert("请选择收件人！");
                return false
            }
            if (!title){
                alert("邮件主题不能为空！");
                return false
            }
            if (!message_content){
                alert("邮件内容不能为空！");
                return false
            }
            var message_id = window.location.href.split("message_id=")
            if (message_id.length>=2){
                form.append('message_id', message_id[1]);
            }

            document.getElementById("loading").style.display='inline-block';
            var xhr=new XMLHttpRequest();
            var local_host = window.location.host;
            xhr.open('post', "/correspondence/send-message", true);
            form.append('title', String(title));
            form.append('content', String(message_content));
            form.append('plain_text', String(Plain_Text));
            form.append('is_draft', String(types));
            form.append('unique', unique);
            form.append('recipient', JSON.stringify(recipient))

            xhr.send(form);
            xhr.onreadystatechange=function(){
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        document.getElementById("loading").style.display='none';
                        var JsonDate = JSON.parse(xhr.responseText);
                        if (JsonDate["code"] === 200){
                            alert(JsonDate["message"]);
                            if (types === "True") {
                                window.location.href='drafts.html';
                            }else {
                                window.location.href='sent.html';
                            }
                        }
                        else {
                            alert(JsonDate["message"]);
                            console.log(JsonDate);
                        }
                    }
                }
            }
        }

        function scrollConversationScreen() {
            $('#selected-div').scrollTop($('#selected-div')[0].scrollHeight);
        }

        function IdentifyPersonnel() {
            var prople_list = [];
            var selected_personnel_list = document.getElementsByClassName('selected-personnel');
            for (var i=0; i<selected_personnel_list.length; i++) {
                prople_list.push(selected_personnel_list[i].innerText)
            }
            document.getElementById("recipient").value = prople_list.join();
            close_model();
        }
    </script>
{% endblock %}
